home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / windman.zip / WINDOS.PAS < prev   
Pascal/Delphi Source File  |  1991-12-13  |  7KB  |  227 lines

  1.  
  2. Capture Buffer Transfer
  3. No error detection/correction
  4.  
  5. Opening capture buffer...
  6. { A set of routines for text window manipulation
  7.   By Bela Lubkin
  8.   Borland International Technical Support
  9.   1/10/85
  10.   (For PC-DOS Turbo Pascal version 2 or greater)
  11.  
  12. Notes: These routines cause extreme blinking on the color monitor.  If anyone
  13.        modifies them to decrease this blinking, please upload your modified
  14.        routines to the Borland SIG on CompuServe - GO BOR
  15.  
  16.        REMEMBER to dispose of your windows, or you will quickly run out of
  17.        heap space.  Procedure DRestoreWindow restores a window to the screen
  18.        and then disposes of the window;  procedure DisposeWindow disposes of
  19.        a window.  DO NOT USE Turbo's built in Dispose procedure on a window;
  20.        windows are allocated with GetMem and must be disposed with FreeMem. }
  21.  
  22. Type
  23.   XTCoord=1..80;   { X Text coordinate }
  24.   YTCoord=1..25;   { Y Text coordinate }
  25.   XTCoord0=0..80;  { X Text coordinate + 0 for nothing }
  26.   YTCoord0=0..25;  { Y Text coordinate + 0 for nothing }
  27.   WindowRec=Record
  28.               XSize: XTCoord;
  29.               YSize: YTCoord;
  30.               XPosn: XTCoord;
  31.               YPosn: YTCoord;
  32.               Contents: Array [0..1999] Of Integer;
  33.             End;
  34.   WindowPtr=^WindowRec;
  35.  
  36. Var
  37.   ScreenBase: Integer;  { Segment address of the screen: $B000 for monochrome,
  38.                           $B800 for color }
  39.   WindowXLo: XTCoord;
  40.   WindowYLo: YTCoord;
  41.   WindowXHi: XTCoord;
  42.   WindowYHi: YTCoord;
  43.  
  44. Procedure TurboWindow(XL: XTCoord; YL: YTCoord; XH: XTCoord; YH: YTCoord);
  45. { This procedure provides an entry to Turbo's built in Window procedure }
  46.   Begin
  47.     Window(XL,YL,XH,YH);
  48.   End;
  49.  
  50. Procedure Window(XL: XTCoord; YL: YTCoord; XH: XTCoord; YH: YTCoord);
  51. { This procedure replaces Turbo's built in Window procedure.  It calls the
  52.   original Window procedure, and also keeps track of the window boundaries. }
  53.  
  54.   Begin
  55.     TurboWindow(XL,YL,XH,YH);
  56.     WindowXLo:=XL;
  57.     WindowYLo:=YL;
  58.     WindowXHi:=XH;
  59.     WindowYHi:=YH;
  60.   End;
  61.  
  62. Function SaveWindow(XLow: XTCoord; YLow: YTCoord;
  63.                     XHigh: XTCoord; YHigh:YTCoord): WindowPtr;
  64. { Allocate a WindowRec of the precise size needed to save the window, then
  65.   fill it with the text that is in the window XLow..XHigh, YLow..YHigh.
  66.   Return a pointer to this WindowRec. }
  67.  
  68.   Var
  69.     SW: WindowPtr;
  70.     I: Integer;
  71.     XS: XTCoord;
  72.     YS: YTCoord;
  73.  
  74.   Begin
  75.     XS:=XHigh-XLow+1;
  76.     YS:=YHigh-YLow+1;
  77.     GetMem(SW,2*XS*YS + 4);
  78.     { Allocate 2 bytes for each screen position, + 4 for size and position }
  79.     With SW^ Do
  80.      Begin
  81.       XSize:=XS;
  82.       YSize:=YS;
  83.       XPosn:=XLow;
  84.       YPosn:=YLow;
  85.       For I:=0 To YSize-1 Do
  86.         Move(Mem[ScreenBase:((YPosn+I-1)*80+XPosn-1) Shl 1],
  87.              Contents[I*XSize],XSize Shl 1);
  88.       { For each line of the window,
  89.           Move XSize*2 bytes (1 for char, 1 for attribute) into the Contents
  90.                array.  Leave no holes in the array. }
  91.      End;
  92.     SaveWindow:=SW;
  93.   End;
  94.  
  95. Function SaveCurrentWindow: WindowPtr;
  96.   Begin
  97.     SaveCurrentWindow:=SaveWindow(WindowXLo,WindowYLo,WindowXHi,WindowYHi);
  98.   End;
  99.  
  100. Procedure RestoreWindow(WP: WindowPtr; XPos: XTCoord0; YPos: YTCoord0);
  101. { Given a pointer to a WindowRec, restore the contents of the window.  If
  102.   XPos or YPos is 0, use the XPosn or YPosn that the window was originally
  103.   saved with.  If either is nonzero, use it.  Thus a window can be restored
  104.   exactly with  RestoreWindow(wp,0,0);  or its upper left corner can be
  105.   placed at (2,3) with  RestoreWindow(wp,2,3); }
  106.  
  107.   Var
  108.     I: Integer;
  109.  
  110.   Begin
  111.     With WP^ Do
  112.      Begin
  113.       If XPos=0 Then XPos:=XPosn;
  114.       If YPos=0 Then YPos:=YPosn;
  115.       For I:=0 To YSize-1 Do
  116.         Move(Contents[I*XSize],
  117.              Mem[ScreenBase:2*((YPos+I-1)*80+XPos-1)],XSize*2);
  118.       { For each line of the window,
  119.           Move XSize*2 bytes (1 for char, 1 for attribute) from the Contents
  120.                array onto the screen. }
  121.      End;
  122.   End;
  123.  
  124. Procedure DisposeWindow(Var WP: WindowPtr);
  125. { Dispose of a WindowPtr.  The built in procedure Dispose cannot be used,
  126.   because it will deallocate SizeOf(WindowRec) bytes, even though less may
  127.   have been allocated. }
  128.  
  129.   Begin
  130.     With WP^ Do FreeMem(WP,2*XSize*YSize+4);
  131.     WP:=Nil;
  132.   End;
  133.  
  134. Procedure DRestoreWindow(Var WP: WindowPtr; XPos: XTCoord0; YPos: YTCoord0);
  135. { Restore the contents of a window, then dispose of the saved image }
  136.  
  137.   Begin
  138.     RestoreWindow(WP, XPos, YPos);
  139.     DisposeWindow(WP);
  140.   End;
  141.  
  142. Procedure DRestoreCurrentWindow(Var WP: WindowPtr;
  143.                                 XPos: XTCoord0; YPos: YTCoord0);
  144. { Restore the contents of a window, set the current window to fit the restored
  145.   window, and dispose of the saved image.  A similar procedure
  146.   RestoreCurrentWindow could be written by changing DRestoreWindow to
  147.   RestoreWindow in the last line of the procedure, but I have assumed that
  148.   when you select a window area, you are going to modify it, and not want the
  149.   old image }
  150.  
  151.   Begin
  152.     With WP^ Do
  153.      Begin
  154.       If XPos=0 Then XPos:=XPosn;
  155.       If YPos=0 Then YPos:=YPosn;
  156.       Window(XPos,YPos,XPos+XSize-1,YPos+YSize-1);
  157.      End;
  158.     DRestoreWindow(WP, XPos, YPos);
  159.   End;
  160.  
  161. Procedure DetermineDisplay;
  162. { Set ScreenBase to $B000 or $B800, depending on which display is in use.
  163.   A side effect is that the cursor is left at (1,1) on the screen. }
  164.  
  165.   Var
  166.     M,C: Integer;
  167.     T: Byte;
  168.  
  169.   Begin
  170.     M:=MemW[$B000:0];
  171.     C:=MemW[$B800:0];
  172.     T:=64;
  173.     If (Hi(M)=T) Or (Hi(C)=T) Then T:=65;
  174.     If (Hi(M)=T) Or (Hi(C)=T) Then T:=66;
  175.     GotoXY(1,1);
  176.     Write(Chr(T));
  177.     GotoXY(1,1);
  178.     If Mem[$B000:0]=T Then ScreenBase:=$B000
  179.     Else ScreenBase:=$B800;
  180.     MemW[$B000:0]:=M;
  181.     MemW[$B800:0]:=C;
  182.   End;
  183.  
  184. { Example program -- remove next line to enable }
  185. (*
  186. Var
  187.   X,Y: Byte;
  188.   W,W2: WindowPtr;
  189.   Ch: Char;
  190.  
  191. Begin
  192.   DetermineDisplay; { Set ScreenBase according to the display in use }
  193.   For Y:=1 To 1999 Do
  194.     Write(Chr(Random(95)+32)); { Fill the screen with junk }
  195.   W:=SaveWindow(1,1,80,25); { Save the whole screen }
  196.   Read(Kbd,Ch);
  197.   ClrScr; { Clear it }
  198.   Read(Kbd,Ch);
  199.   DRestoreWindow(W,0,0); { Restore it }
  200.   Read(Kbd,Ch);
  201.   Window(5,4,53,23);
  202.   W:=SaveCurrentWindow; { Save a medium sized window }
  203.   ClrScr; { Wipe that window }
  204.   Read(Kbd,Ch);
  205.   RestoreWindow(W,0,0); { And restore it }
  206.   Window(1,1,80,25);
  207.   Read(Kbd,Ch);
  208.   W2:=SaveWindow(2,2,10,10); { Save a small window }
  209.   ClrScr;
  210.   Read(Kbd,Ch);
  211.   For X:=1 To 72 Do          { Restore it in a square around the edges of }
  212.     RestoreWindow(W2,X,1);   {  the screen }
  213.   For Y:=2 To 17 Do
  214.     RestoreWindow(W2,72,Y);
  215.   For X:=71 DownTo 1 Do
  216.     RestoreWindow(W2,X,17);
  217.   For Y:=16 DownTo 1 Do
  218.     RestoreWindow(W2,1,Y);
  219.   Read(Kbd,Ch);
  220.   DisposeWindow(W2);
  221.   DRestoreWindow(W,0,0); { Restore the medium sized window saved earlier }
  222. End.
  223. (**)
  224. Capture buffer closed.
  225.  
  226. Key <ENTER> to continue: 
  227. w(W,0,0); { Restore the m